package cz.worsik.android.test.rideshare.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cz.worsik.android.test.rideshare.R;
import cz.worsik.android.test.rideshare.constants.InitConst;
import cz.worsik.android.test.rideshare.model.DrawerItem;
import cz.worsik.android.test.rideshare.model.Route;
import cz.worsik.android.test.rideshare.model.RouteSpinnerItem;
import cz.worsik.android.test.rideshare.model.list.ERouteType;

/**
 * Created by 231260 on 3.1.14.
 */
public class DrawerAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<DrawerItem> items;

    public DrawerAdapter(Context context, ArrayList<DrawerItem> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        DrawerItemHolder drawerHolder;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            drawerHolder = new DrawerItemHolder();

            view = (View) inflater.inflate(
                    R.layout.drawer_item, parent, false);
            drawerHolder.text = (TextView) view.findViewById(R.id.item_title);
            drawerHolder.icon = (ImageView)view.findViewById(R.id.item_icon);

            drawerHolder.spinner = (Spinner) view
                    .findViewById(R.id.drawerSpinner);

            drawerHolder.title = (TextView) view.findViewById(R.id.drawerTitle);

            drawerHolder.headerLayout = (LinearLayout) view
                    .findViewById(R.id.headerLayout);
            drawerHolder.itemLayout = (LinearLayout) view
                    .findViewById(R.id.itemLayout);
            drawerHolder.spinnerLayout = (LinearLayout) view
                    .findViewById(R.id.spinnerLayout);

            view.setTag(drawerHolder);
        } else {
            drawerHolder = (DrawerItemHolder) view.getTag();
        }

        DrawerItem item = this.items.get(position);

        if (item.isList()) {
            drawerHolder.headerLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.spinnerLayout.setVisibility(LinearLayout.VISIBLE);

            List<RouteSpinnerItem> routeList = InitConst.getRouteListForSpinner(context.getResources());

            ArrayAdapter<RouteSpinnerItem> adapter = new RouteItemListAdapter((Activity) context, routeList);

            drawerHolder.spinner.setAdapter(adapter);

            drawerHolder.spinner
                    .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0,
                                                   View arg1, int position, long arg3) {

                            switch (InitConst.getRouteList(arg0.getResources()).get(position).getType())
                            {
                                case NEW:
                                    Toast.makeText(context, "New route",
                                            Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                                    Toast.makeText(context, "Route Changed",
                                            Toast.LENGTH_SHORT).show();
                                    break;
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }
                    });

        } else if (item.getTitle() != null) {
            drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.spinnerLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.title.setText(item.getTitle());

        } else {

            drawerHolder.headerLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.spinnerLayout.setVisibility(LinearLayout.GONE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);

            drawerHolder.text.setText(item.getText());
            drawerHolder.icon.setImageDrawable(item.getIcon());
            drawerHolder.icon.setContentDescription("Menu icon");
        }

        return view;
    }

    private static class DrawerItemHolder {
        TextView text, title;
        ImageView icon;
        LinearLayout headerLayout, itemLayout, spinnerLayout;
        Spinner spinner;
    }
}
